package com.tuanzi.loan.business.analyzer;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.tuanzi.loan.vo.view.family.UserValidateView;
import com.tuanzi.server.loan_base.util.PhoneNumNideUtil;

import lombok.extern.slf4j.Slf4j;

import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;

/**
 * BaiduYYS类型的运营商数据报告
 * @author JinRuiyang
 */
@Slf4j
@Component
public class BaiduYYSAnalyzer {

    public List<UserValidateView.CallLog> getCallLogsAndIspRegTime(String json, UserValidateView.UserAuthInfo authInfo){
        List<UserValidateView.CallLog> result = new ArrayList<>();
        if (StringUtils.isBlank(json))
            return result;
        JSONObject jsonObject = JSON.parseObject(json);
        Integer errno = jsonObject.getInteger("errno");
        if (errno == null || errno != 0)
            return result;
        try {
            JSONObject data = jsonObject.getJSONObject("data").getJSONObject("9");
            Integer reportStatus = data.getInteger("report_status");
            if (reportStatus == null || reportStatus != 2)
                return result;
            JSONObject report = data.getJSONObject("report_content");
            JSONObject basic = report.getJSONObject("basic");
            JSONArray calls = report.getJSONArray("calls");
            authInfo.setPhoneNetAccessTime(basic.getString("reg_time"));
            for (int i = 0; i < calls.size(); i++) {
                JSONObject call = calls.getJSONObject(i);
                UserValidateView.CallLog callLog = new UserValidateView.CallLog();
                callLog.setCallDate(call.getString("start_time"));
                callLog.setCallTime(call.getString("use_time"));
                callLog.setCallType(call.getString("init_type"));
                callLog.setPhoneNumber(PhoneNumNideUtil.hideNum(call.getString("other_cell_phone")));
                callLog.setPhoneNumberHomeLocation(call.getString("place"));
                result.add(callLog);
            }
        }
        catch (Exception e){
            return result;
        }
        return result;
    }

}
